package com.credithc.customers.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.TypeReference;
import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.customers.api.service.IPreCustomerService;
import com.credithc.customers.api.utils.RequestUtils;
import com.credithc.customers.base.codes.CustomerCodes;
import com.credithc.customers.base.exception.CustomerException;
import com.credithc.customers.base.ro.BaseBatchRequest;
import com.credithc.customers.base.ro.BaseRequest;
import com.credithc.customers.base.ro.BatchPreCustomerRo;
import com.credithc.customers.base.ro.PreCustomerRo;
import com.credithc.framework.log.annotation.Trace;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author zhukai
 * @date 2018/11/28 16:30
 */
@RestController
@RequestMapping("/api/yzt/v1")
@Slf4j
public class PreCustomerController {

    private final MessageFactory messageFactory;

    private final IPreCustomerService preCustomerService;

    private final RequestUtils requestUtils;

    @Autowired
    public PreCustomerController(MessageFactory messageFactory, IPreCustomerService preCustomerService, RequestUtils requestUtils) {
        this.messageFactory = messageFactory;
        this.preCustomerService = preCustomerService;
        this.requestUtils = requestUtils;
    }

    @PostMapping("/preCustomerNo")
    @Trace
    public Message createPreCustomerNo(HttpServletRequest request) {
        try {
            String requestStr = RequestUtils.generateRequest(request);
            BaseRequest<PreCustomerRo> bRequest = JSON.parseObject(requestStr, new TypeReference<BaseRequest<PreCustomerRo>>() {
            });
            requestUtils.checkRequestBaseParam(requestStr);
            Map<String, String> map = preCustomerService.createPreCustomerNo(bRequest);
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, map);
        } catch (IOException e) {
            throw new CustomerException(CustomerCodes.READ_REQUEST_ERROR);
        } catch (JSONException e) {
            log.info("创建用户接口参数格式错误，异常信息：{}", e.getMessage());
            throw new CustomerException(CustomerCodes.FAST_JSON_ERROR);
        }
    }

    @PostMapping("/preCustomerNo/batch")
    public Message batchCreatePreCustomerNo(HttpServletRequest request) {
        long start = System.currentTimeMillis();
        try {
            String requestStr = RequestUtils.generateRequest(request);
//            log.info("==========参数============" + requestStr);
            BaseBatchRequest<BatchPreCustomerRo> bRequest = JSON.parseObject(requestStr, new TypeReference<BaseBatchRequest<BatchPreCustomerRo>>() {
            });
            requestUtils.checkRequestBaseParam(requestStr);
            List<Map<String, String>> list = preCustomerService.batchCreateCustomerNo(bRequest);
            log.info("===================spend============"+(System.currentTimeMillis()- start));
            return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, list);
        } catch (IOException e) {
            throw new CustomerException(CustomerCodes.READ_REQUEST_ERROR);
        } catch (JSONException e) {
            log.info("批量创建用户接口参数格式错误，异常信息：{}", e.getMessage());
            throw new CustomerException(CustomerCodes.FAST_JSON_ERROR);
        }

    }


}
